﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SksApp
{
    public partial class ListView : BaseView
    {
        public enum ListeTipleri
        {
            Stok, Musteri
        }
        public ListeTipleri ListeTipi { get; set; }

        public ListView()
        {
            InitializeComponent();
            this.Load += new EventHandler(ListView_Load);
        }

        void ListView_Load(object sender, EventArgs e)
        {
            Liste(null, null);
        }

        public void Liste(object sender, EventArgs e)
        {
            using (SksEntity ent = new SksEntity())
            {
                switch (ListeTipi)
                {
                    case ListeTipleri.Stok:
                        lblTitle.Text = "Stok Kayıtları";
                        dataGridView1.DataSource = ent.Stoklar.Select(x => new { x.ID, x.Kodu, x.Adi, x.Fiyat1, GrupKodu1 = x.Filtre1 }).Take(1000).ToList();
                        break;
                    case ListeTipleri.Musteri:
                        lblTitle.Text = "Müşteri Kayıtları";
                        dataGridView1.DataSource = ent.Musteriler.Select(x => new { x.ID, x.Kodu, x.Adi, x.VergiDairesi, x.VergiNo, x.Adres }).Take(1000).ToList();
                        break;
                }
                dataGridView1.Columns[0].Visible = false;
                dataGridView1.AutoResizeColumns();
            }
        }

        private void btnKapat_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Event.Invoke(null, null);
        }

        private void btnEkle_Click(object sender, EventArgs e)
        {
            switch (ListeTipi)
            {
                case ListeTipleri.Stok:
                    using (frmStok stk = new frmStok())
                    {
                        stk.Event = Liste;
                        stk.ShowDialog(this);
                    }
                    break;
                case ListeTipleri.Musteri:
                    using (frmMusteri ms = new frmMusteri())
                    {
                        ms.Event = Liste;
                        ms.ShowDialog(this);
                    }
                    break;
            }
        }

        private void StokSil(string stokkodu)
        {
            if (!string.IsNullOrEmpty(stokkodu))
            {
                using (SksEntity ent = new SksEntity())
                {
                    Stok stk = ent.Stoklar.Where(x => x.Kodu == stokkodu).FirstOrDefault();
                    if (stk != null)
                    {
                        foreach (Barkod b in ent.Barkodlar.Where(x => x.StokID == stk.ID).ToList())
                        {
                            ent.Barkodlar.Remove(b);
                        }
                        ent.Stoklar.Remove(stk);
                        ent.SaveChanges();
                    }
                }
                Liste(null, null);
            }
        }

        private void CariSil(string carikod)
        {
            using (SksEntity ent = new SksEntity())
            {
                Musteri ms = ent.Musteriler.Where(x => x.Kodu == carikod).FirstOrDefault();
                if (ms != null)
                {
                    ent.Musteriler.Remove(ms);
                }
                ent.SaveChanges();
            }
            Liste(null, null);
        }

        private void btnDegistir_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null && dataGridView1.CurrentRow.Index == -1)
                return;

            switch (ListeTipi)
            {
                case ListeTipleri.Stok:
                    using (frmStok stk = new frmStok())
                    {
                        stk.Event = Liste;
                        stk.StokKodu = dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString();
                        stk.ShowDialog(this);
                    }
                    break;
                case ListeTipleri.Musteri:
                    using (frmMusteri ms = new frmMusteri())
                    {
                        ms.Event = Liste;
                        ms.CariKodu = dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString();
                        ms.ShowDialog(this);
                    }
                    break;
            }
        }

        private void btnSil_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null && dataGridView1.CurrentRow.Index == -1)
                return;

            switch (ListeTipi)
            {
                case ListeTipleri.Stok:
                    StokSil(dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString());
                    break;
                case ListeTipleri.Musteri:
                    CariSil(dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString());
                    break;
            }
        }
    }
}
